//---------------------------------------------------------------------
// File: ReceiveLocationEnabledStep.cs
// 
// Summary: 
//---------------------------------------------------------------------
using System;
using System.Xml;
using BizUnit.BizUnitOM;
using Microsoft.BizTalk.ExplorerOM;

namespace BizUnit.Extensions.BizTalk
{
    using Helpers;
    /// <summary>
    /// The SendPortStatusStep test step checks to determine whether a specific send port is started or stopped.
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    ///	<TestStep assemblyPath="BizUnit.Extensions.BizTalk.dll" typeName="BizUnit.Extensions.BizTalk.SendPortStatusStep">
    ///		<SendPortName>GovenorIn</SendPortName>
    ///		<Status>Started</Status>
    ///	</TestStep>
    ///	</code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>SendPortNme</term>
    ///			<description>The name of the send port to check</description>
    ///		</item>
    ///		<item>
    ///			<term>Status</term>
    ///			<description>Check if it is Bound,Started,Stopped</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class SendPortVerifyStatusStep : ITestStep, ITestStepOM
    {
        private BtsCatalogExplorer root;
        private IBizTalkApplication appInstance;
        private SendPort sendPortInstance;

        private string applicationName;
        private string connectionString;
        private string sendPortName;

        private PortStatus statusToVerify;
        public string ApplicationName
        {
            get
            {
                return applicationName;
            }
            set
            {
                applicationName = value;
            }
        }
        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
            set
            {
                connectionString = value;
            }
        }
        public string SendPortName
        {
            get
            {
                return sendPortName;
            }
            set
            {
                sendPortName = value;
            }
        }
        public PortStatus StatusToVerify
        {
            get
            {
                return statusToVerify;
            }
            set
            {
                statusToVerify = value;
            }
        }
        /// <summary>
        /// ITestStep.Execute() implementation
        /// </summary>
        /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
        /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
        public void Execute(XmlNode testConfig, Context context)
        {
            ApplicationName = context.ReadConfigAsString(testConfig, "ApplicationName");
            ConnectionString = context.ReadConfigAsString(testConfig, "ConnectionString");
            SendPortName = context.ReadConfigAsString(testConfig, "SendPortName");
            StatusToVerify = (PortStatus)Enum.Parse(typeof(PortStatus), context.ReadConfigAsString(testConfig, "StatusToVerify"), true);
            Execute(context);

        }





        #region ITestStepOM Members

        public void Execute(Context context)
        {
            Validate(context);
            appInstance = BizTalkStepHelper.GetApplication(ApplicationName, ConnectionString);
            sendPortInstance = BizTalkStepHelper.GetSendPort(appInstance, SendPortName);


            if (StatusToVerify != sendPortInstance.Status)
            {
                throw new ApplicationException(string.Format("The send port: {0} was expected to be in Status {1}  but was in status {2} ", SendPortName, statusToVerify, sendPortInstance.Status.ToString()));
            }
            else
            {
                context.LogInfo(string.Format("The sendport: {0} was found to be in the required status {1} ", SendPortName, sendPortInstance.Status.ToString()));
            }

        }

        public void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(ApplicationName, "Application Name");
            ArgumentValidation.CheckForEmptyString(ConnectionString, "Connection String");
            ArgumentValidation.CheckForEmptyString(SendPortName, "SendPort Name");
            ArgumentValidation.CheckForEmptyString(StatusToVerify.ToString(), "Status To Verify");

        }

        #endregion
    }
}
